﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SRM.DataObjects;
using Telerik.Web.UI;
using System.Data;
using SRM;

public partial class UserControls_SkillSetUserControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    private bool ValidateFormFields(string mode)
    {
        bool hasFilledAllRequiredFields = true;

        if (mode == "UPDATE")
        {
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(SkillSet_ObjectDataSource.UpdateParameters["skillCategory"].DefaultValue)))
                hasFilledAllRequiredFields = false; // Language Proficiency not filled
        }

        if (mode == "INSERT")
        {
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(SkillSet_ObjectDataSource.InsertParameters["skillCategory"].DefaultValue)))
                hasFilledAllRequiredFields = false; // Language Proficiency not filled
        }

        return hasFilledAllRequiredFields;
    }

    protected void rgSkillSet_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.UpdateCommandName)
        {
            GridEditFormItem item = e.Item as GridEditFormItem;
            SkillSet_ObjectDataSource.UpdateParameters["skillSetKey"].DefaultValue = SRM.Help.GetHiddenFieldValue(item.FindControl("skillSetKey"));
            SkillSet_ObjectDataSource.UpdateParameters["skillCategory"].DefaultValue = SRM.Help.GetRadComboBoxValue(item.FindControl("skillcategory_combobox"));
            SkillSet_ObjectDataSource.UpdateParameters["skillHeader"].DefaultValue = SRM.Help.GetRadComboBoxValue(item.FindControl("skillheader_combobox"));
            SkillSet_ObjectDataSource.UpdateParameters["skillDetail"].DefaultValue = SRM.Help.GetRadComboBoxValue(item.FindControl("skilldetail_combobox"));
            SkillSet_ObjectDataSource.UpdateParameters["skillOthers"].DefaultValue = SRM.Help.GetTextBoxValue(item.FindControl("skillothers_textbox"));
            
            if (ValidateFormFields("UPDATE"))
            {
                SkillSet_ObjectDataSource.Update();
            }
            else
            {
                e.Canceled = true;
                System.Web.UI.WebControls.Literal msg = item.FindControl("SkillSetMessage") as System.Web.UI.WebControls.Literal;
                if (msg != null)
                {
                    msg.Text = "<p class='warning'>Please fill in required fields.</p>";
                }
            }
        }

        if (e.CommandName == RadGrid.DeleteCommandName)
        {
            GridDataItem item = e.Item as GridDataItem;
            SkillSet_ObjectDataSource.DeleteParameters["skillSetKey"].DefaultValue = item["SkillSetKey_column"].Text;
            SkillSet_ObjectDataSource.Delete();
        }

        if (e.CommandName == RadGrid.PerformInsertCommandName)
        {
            GridEditFormInsertItem item = e.Item as GridEditFormInsertItem;
            SkillSet_ObjectDataSource.InsertParameters["skillCategory"].DefaultValue = SRM.Help.GetRadComboBoxValue(item.FindControl("skillcategory_combobox"));
            SkillSet_ObjectDataSource.InsertParameters["skillHeader"].DefaultValue = SRM.Help.GetRadComboBoxValue(item.FindControl("skillheader_combobox"));
            SkillSet_ObjectDataSource.InsertParameters["skillDetail"].DefaultValue = SRM.Help.GetRadComboBoxValue(item.FindControl("skilldetail_combobox"));
            SkillSet_ObjectDataSource.InsertParameters["skillOthers"].DefaultValue = SRM.Help.GetTextBoxValue(item.FindControl("skillothers_textbox"));
            
            if (ValidateFormFields("INSERT"))
            {
                SkillSet_ObjectDataSource.Insert();
            }
            else
            {
                e.Canceled = true;
                System.Web.UI.WebControls.Literal msg = item.FindControl("SkillSetMessage") as System.Web.UI.WebControls.Literal;
                if (msg != null)
                {
                    msg.Text = "<p class='warning'>Please fill in required fields.</p>";
                }
            }
        }
    }
    
    protected void SkillCategory_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
    {
        Telerik.Web.UI.RadComboBox skillcategory_combobox = null;
        Telerik.Web.UI.RadComboBox skillheader_combobox = null;
        Telerik.Web.UI.RadComboBox skilldetail_combobox = null;
        System.Web.UI.WebControls.TextBox skillothers_textbox = null;

        skillcategory_combobox = sender as Telerik.Web.UI.RadComboBox;
        skillheader_combobox = skillcategory_combobox.Parent.FindControl("skillheader_combobox") as Telerik.Web.UI.RadComboBox;
        skilldetail_combobox= skillcategory_combobox.Parent.FindControl("skilldetail_combobox") as Telerik.Web.UI.RadComboBox;
        skillothers_textbox = skillcategory_combobox.Parent.FindControl("skillothers_textbox") as System.Web.UI.WebControls.TextBox;

        skillheader_combobox.Items.Clear();
        skillheader_combobox.Items.Add(new RadComboBoxItem("Please select", string.Empty));
        skillheader_combobox.Text = "";

        skilldetail_combobox.Items.Clear();
        skilldetail_combobox.Items.Add(new RadComboBoxItem("Please select", string.Empty));
        skilldetail_combobox.Text = "";

        skillheader_combobox.DataTextField = "DisplayText";
        skillheader_combobox.DataValueField = "Id";
        skillheader_combobox.DataSource = SRM_Code.GetSkillHeader("1", skillcategory_combobox.SelectedValue); ;
        skillheader_combobox.DataBind();

        // find others row
        System.Web.UI.HtmlControls.HtmlTableRow skillset_other_row = skillcategory_combobox.Parent.FindControl("skillset_other_row") as System.Web.UI.HtmlControls.HtmlTableRow;
        System.Web.UI.HtmlControls.HtmlTableRow skillset_header_row = skillcategory_combobox.Parent.FindControl("skillset_header_row") as System.Web.UI.HtmlControls.HtmlTableRow;
        System.Web.UI.HtmlControls.HtmlTableRow skillset_detail_row = skillcategory_combobox.Parent.FindControl("skillset_detail_row") as System.Web.UI.HtmlControls.HtmlTableRow;

        if (skillset_other_row != null)
        {
            
            if (skillcategory_combobox.SelectedValue == "999")
            {
                skillset_header_row.Visible = false;
                skillset_detail_row.Visible = false;
                skillset_other_row.Visible = true;
                skillothers_textbox.Text = string.Empty;
            }
            else
            {
                skillset_header_row.Visible = true;
                skillset_detail_row.Visible = true;
                skillset_other_row.Visible = false;
                skillothers_textbox.Text = string.Empty;
            }
        }
    }

    protected void SkillHeader_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
    {
        Telerik.Web.UI.RadComboBox skillcategory_combobox = null;
        Telerik.Web.UI.RadComboBox skillheader_combobox = null;
        Telerik.Web.UI.RadComboBox skilldetail_combobox = null;

        
        skillheader_combobox = sender as Telerik.Web.UI.RadComboBox;
        skillcategory_combobox = skillheader_combobox.Parent.FindControl("skillcategory_combobox") as Telerik.Web.UI.RadComboBox;
        skilldetail_combobox = skillheader_combobox.Parent.FindControl("skilldetail_combobox") as Telerik.Web.UI.RadComboBox;

        skilldetail_combobox.Items.Clear();
        skilldetail_combobox.Items.Add(new RadComboBoxItem("Please select", string.Empty));
        skilldetail_combobox.Text = "";

        skilldetail_combobox.DataTextField = "DisplayText";
        skilldetail_combobox.DataValueField = "Id";
        skilldetail_combobox.DataSource = SRM_Code.GetSkillDetail("1", skillcategory_combobox.SelectedValue, skillheader_combobox.SelectedValue); ;
        skilldetail_combobox.DataBind();
    }

    protected void rgSkillSet_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
        {
            DataTable dt = null;
            GridEditFormItem geiEditedItem = e.Item as GridEditFormItem;
            geiEditedItem.Visible = true;

            Telerik.Web.UI.RadComboBox skillcategory_combobox = null;
            Telerik.Web.UI.RadComboBox skillheader_combobox = null;
            Telerik.Web.UI.RadComboBox skilldetail_combobox = null;
            System.Web.UI.WebControls.TextBox skillothers_textbox = null;

            skillcategory_combobox = geiEditedItem.FindControl("skillcategory_combobox") as Telerik.Web.UI.RadComboBox;
            skillheader_combobox = geiEditedItem.FindControl("skillheader_combobox") as Telerik.Web.UI.RadComboBox;
            skilldetail_combobox = geiEditedItem.FindControl("skilldetail_combobox") as Telerik.Web.UI.RadComboBox;
            skillothers_textbox = geiEditedItem.FindControl("skillothers_textbox") as System.Web.UI.WebControls.TextBox;

            // Databind the 3 combo-boxes 
            Help.BindSapCodeToComboBox(skillcategory_combobox, SRM_Code.GetSkillCategory("1"));
            if ((e.Item.DataItem != null) && (e.Item.DataItem is DataRowView))
            {
                //skillcategory_combobox.SelectedValue = ((DataRowView)e.Item.DataItem).Row["SkillCategory"].ToString();
                Help.SetComboBoxSelectedValue(skillcategory_combobox, ((DataRowView)e.Item.DataItem).Row["SkillCategory"].ToString());
            }
                

            Help.BindSapCodeToComboBox(skillheader_combobox, SRM_Code.GetSkillCategory("1"));
            if ((e.Item.DataItem != null) && (e.Item.DataItem is DataRowView))
            {
                //skillheader_combobox.SelectedValue = ((System.Data.DataRowView)e.Item.DataItem).Row["SkillHeader"].ToString();
                Help.SetComboBoxSelectedValue(skillheader_combobox, ((DataRowView)e.Item.DataItem).Row["SkillHeader"].ToString());
            }
                

            Help.BindSapCodeToComboBox(skilldetail_combobox, SRM_Code.GetSkillCategory("1"));
            if ((e.Item.DataItem != null) && (e.Item.DataItem is DataRowView))
            {

                //skilldetail_combobox.SelectedValue = ((System.Data.DataRowView)e.Item.DataItem).Row["SkillDetail"].ToString();
                Help.SetComboBoxSelectedValue(skilldetail_combobox, ((DataRowView)e.Item.DataItem).Row["SkillDetail"].ToString());
            }
                

            // find others row
            System.Web.UI.HtmlControls.HtmlTableRow skillset_other_row = skillcategory_combobox.Parent.FindControl("skillset_other_row") as System.Web.UI.HtmlControls.HtmlTableRow;
            System.Web.UI.HtmlControls.HtmlTableRow skillset_header_row = skillcategory_combobox.Parent.FindControl("skillset_header_row") as System.Web.UI.HtmlControls.HtmlTableRow;
            System.Web.UI.HtmlControls.HtmlTableRow skillset_detail_row = skillcategory_combobox.Parent.FindControl("skillset_detail_row") as System.Web.UI.HtmlControls.HtmlTableRow;

            if (skillset_other_row != null)
            {

                if (skillcategory_combobox.SelectedValue == "999")
                {
                    skillset_header_row.Visible = false;
                    skillset_detail_row.Visible = false;
                    skillset_other_row.Visible = true;
                    skillothers_textbox.Text = ((System.Data.DataRowView)e.Item.DataItem).Row["Others"].ToString();
                }
                else
                {
                    skillset_header_row.Visible = true;
                    skillset_detail_row.Visible = true;
                    skillset_other_row.Visible = false;
                    skillothers_textbox.Text = string.Empty;
                }
            }

        }
    }
}